<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="年度：">
          <el-select v-model="listPram.year" placeholder="年度" clearable class="selWidth">
            <el-option
              v-for="item in yearList"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目搜索：">
          <el-input v-model="listPram.name" placeholder="请输入项目名称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)" v-hasPermi="['admin:project:save']">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" class="table" v-loading="loading" size="mini" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column
            prop="year"
            label="年度"
            min-width="50"
        />
        <el-table-column
          prop="name"
          label="项目名称"
          min-width="150"
        />
        <el-table-column
            label="类别"
            min-width="60"
        >
          <template slot-scope="scope">
            <el-tag size="small" type="info" class="mr5">
              {{ scope.row.typeText }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="项目类型"
          min-width="60"
        >
          <template slot-scope="scope">
            <el-tag size="small" type="info" class="mr5">
              {{ scope.row.classText }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="departmentName"
            label="责任单位及包抓领导"
            min-width="120"
        >
          <template slot-scope="scope">
            <p>{{scope.row.departmentText}}</p>
            <p>{{scope.row.leader}}</p>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="statusText" min-width="70">
<!--          <template slot-scope="scope">-->
<!--            <el-select v-model="scope.row.status" placeholder="蓝/黄/红项目" clearable style="width: 200px" @change="onChange(scope.row)">-->
<!--              <el-option-->
<!--                v-for="(item,index) in statusList"-->
<!--                :key="index"-->
<!--                :label="item.name"-->
<!--                :value="item.id"-->
<!--                :style="'color:'+handleGetLightColor(item.id)"-->
<!--              />-->
<!--            </el-select>-->
<!--          </template>-->

          <template slot-scope="scope">
<!--            <el-button type="text" size="mini" @click="handlerOpenRead(scope.row)" v-hasPermi="['admin:project:info']">-->
              <span :style="'color:'+handleGetLightColor(scope.row.status)">{{scope.row.statusText}}</span>
<!--            </el-button>-->
          </template>
        </el-table-column>
        <!-- <el-table-column label="状态" prop="statusText" min-width="60">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handlerOpenRead(scope.row)" v-hasPermi="['admin:project:info']">
              <span :style="'color:'+handleGetLightColor(scope.row.status)">{{scope.row.statusText}}</span>
            </el-button>
          </template>
        </el-table-column> -->
        <el-table-column label="排序" prop="sort" min-width="50"/>
        <el-table-column label="操作" min-width="130" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" @click="handlerOpenEdit(1,scope.row)" v-hasPermi="['admin:project:info']">编辑</el-button>
            <el-button icon="el-icon-refresh" type="success" size="mini" @click="updateStatus(scope.row.id)" v-hasPermi="['admin:project:update:status']">状态更新</el-button>
            <el-button icon="el-icon-delete" type="danger" size="mini" @click="handlerOpenDel(scope.row)" v-hasPermi="['admin:project:delete']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!--新增/编辑-->
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0? '创建重点投资项目':'编辑重点投资项目'"
      destroy-on-close
      :close-on-click-modal="false"
    >
      <edit
        v-if="editDialogConfig.visible"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        :all-year-list="yearList"
        :all-type-list="typeList"
        :all-category-list="categoryList"
        :all-department-list="departmentList"
        :all-status-list="statusList"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
    <!--状态更新-->
    <el-dialog
        title="状态更新"
        :visible.sync="VisibleStatus"
        width="1100px"
        :close-on-click-modal="false"
        :before-close="handleStatusClose">
      <el-form :model="statusValidateForm" :rules="rules" ref="statusValidateForm" label-width="90px" class="demo-dynamic" v-loading="loadingStatus">
        <el-form-item
            label="状态"
            prop="status"
        >
          <el-radio-group v-model="statusValidateForm.status">
            <el-radio
              v-for="item in statusList"
              :key="item.id"
              :value="item.name"
              :label="item.id">{{item.name}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
            label="进展情况"
            prop="remarks"
        >
          <el-input v-model="statusValidateForm.remarks" type="textarea" :rows="10" placeholder="进展情况"/>
<!--          <Tinymce v-model="statusValidateForm.remarks" :height="200"></Tinymce>-->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleStatusClose">取 消</el-button>
        <el-button type="primary" :loading="loadingBtn" @click="submitStatusForm('statusValidateForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!--状态更新历史记录-->
    <el-dialog
        title="状态更新"
        :visible.sync="dialogVisible"
        :before-close="Close"
        width="900px"
    >
      <el-table :data="processList" v-loading="loading" size="mini" height="400px" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column label="更新时间" prop="updateTime" min-width="60"/>
        <el-table-column label="状态" prop="statusText" min-width="50"/>
        <el-table-column label="进展情况" prop="remarks" min-width="300">
          <template slot-scope="scope">
            <div>{{scope.row.remarks}}</div>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import edit from './edit'
import { checkPermi } from "@/utils/permission";
import * as selfUtil from "@/utils/ZBKJIutil";
import * as categoryApi from "@/api/categoryApi";
import * as projectApi from "@/api/projectApi";
import {staticCategoryList, staticGuaranteeMethodList, staticProjectStatusList, staticTypeList} from '@/utils/constants'
import {Debounce} from "@/utils/validate";
import Tinymce from '@/components/Tinymce/index'
import {categoryProjectStatusList} from "@/api/categoryApi";
import { getLightColor } from "@/utils";
export default {
  // name: "index"
  components: { edit, Tinymce },
  data() {
    return {
      constants:this.$constants,
      loading:false,
      listData: { list: [] },
      listPram: {
        name: '',
        year:'',
        /*ipAddress: null,
        macAddress: null,
        personId: null,*/
        page: 1,
        limit: this.$constants.page.limit[0]
      },
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      },
      yearList:[],
      typeList:[],
      categoryList:[],
      statusList:[],
      departmentList:[],
      id:0,
      Visible:false,

      //状态更新
      VisibleStatus: false,
      statusValidateForm: {
        status: null,
        remarks: '',
        projectId:0,
      },
      rules: {
        status: [{ required: true, message: '请选择状态', trigger: ['blur', 'change'] }],
        remarks: [{ required: true, message: '请填进展情况', trigger: ['blur'] }],
      },
      loadingStatus: false,
      loadingBtn: false,

      //状态更新记录
      dialogVisible: false,//已读状态dialog
      processList:[],
    }
  },
  mounted() {
    this.handleGetProjectList()
    this.initYearList()
    this.initTypeList()
    this.initCategoryList()
    this.initStatusList()
    this.initDepartmentList()
  },
  methods: {
    checkPermi,
    //控制长文本显示和收起
    showMore (row, flag) {
      this.$set(row,'show',flag)
    },

    handleSearch() {
      this.listPram.page = 1
      this.handleGetProjectList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetProjectList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetProjectList()
    },
    handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id}
        projectApi.proDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetProjectList()
        })
      })
    },
    initYearList(){
      this.loading = true

      let myDate = new Date();
      let thisYear = myDate.getFullYear();
      for (let i=0;i<5;i++){
        this.yearList.push(thisYear-i)
      }
      this.loading = false
    },
    initTypeList(){
      this.loading = true

      categoryApi.categoryProjectTypeList().then(data => {
        this.typeList = data
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },
    initCategoryList(){
      this.loading = true

      categoryApi.categoryProjectCategoryList().then(data => {
        this.categoryList = data
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },
    initStatusList(){
      this.loading = true

      categoryApi.categoryProjectStatusList().then(data => {
        this.statusList = data
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },
    initDepartmentList(){
      this.loading = true

      projectApi.getDepartmentOption().then(data => {
        this.departmentList = data
        this.loading = false
      }).catch(()=>{
        this.loading = false
      })
    },
    handleGetProjectList() {
      projectApi.getList( this.listPram ).then(data => {
        this.listData = data
      })
    },
    handleGetLightColor(status){
      return getLightColor(status)
    },

    handlerOpenEdit(isCreate,editDate) {
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetProjectList()
    },
    resetQuery(){
      this.listPram.name = '';
      this.listPram.year = ''
      this.handleGetProjectList()
    },

    //状态更新
    updateStatus(id) {
      this.statusValidateForm = {
        status: null,
        remarks: '',
        projectId:id,
      }
      this.VisibleStatus = true
    },
    handleStatusClose() {
      this.VisibleStatus = false
      this.statusValidateForm = {
        status: null,
        remarks: '',
        projectId:0,
      }
    },
    submitStatusForm:Debounce(function(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loadingBtn = true
          projectApi.updateStatus(this.statusValidateForm).then(res => {
            this.$message.success('更新成功')
            this.loadingBtn = false
            this.handleStatusClose()
            this.handleGetProjectList()
          }).catch(() => {
            this.loadingBtn = false
          })
        } else {
          return false
        }
      })
    }),

    //状态更新记录
    handlerOpenRead(editDate){
      projectApi.getProcessList( editDate.id ).then(data => {
        this.processList = data
      })

      //this.processList = editDate.processList
      this.dialogVisible = true
    },
    Close() {
      this.dialogVisible = false
    },
  }
}
</script>

<style scoped lang="scss">
  .table .showMore{
    display:block;
    width:100%;
    word-break:keep-all;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
  }
</style>
